package pack.afa;

import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class AmazonFreeAppOfTheDayWidgetActivity extends Activity {
    private static final String ADFREE_URL="market://details?id=pack.afapremium";
    private static final String VOTE_URL="market://details?id=pack.afa";
    // private static final String AMAZON_URL="http://www.amazon.com/gp/mas/dl/android?s=";
    private static final String packageName="com.amazon.venezia";
    public static final int[] LOOKUP_TITLES=new int[]{R.string.main_text1A, R.string.main_text1B, R.string.main_text1C,
	R.string.main_text1D, R.string.main_text1E, R.string.main_text1F, R.string.main_text1G };
    public static final int[] LOOKUP_TEXT=new int[]{R.string.main_text3A, R.string.main_text3B, R.string.main_text3C,
	R.string.main_text3D, R.string.main_text3E, R.string.main_text3F, R.string.main_text3G };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	String name=getIntent().getExtras().getString(WidgetProvider.INTENT_EXTRA_NAME);
	String url=getIntent().getExtras().getString(WidgetProvider.INTENT_EXTRA_URL);
	goToAmazon(url);
	showAd(name); // prepare ad in background

    }

    private void showAd(final String appName) {
	setContentView(R.layout.main);
	Random rdmGen=new Random();
	int i=rdmGen.nextInt(LOOKUP_TITLES.length);
	TextView title=(TextView) this.findViewById(R.id.mainTextViewTitle);
	title.setText(LOOKUP_TITLES[i]);
	TextView text=(TextView) this.findViewById(R.id.mainTextViewText);
	text.setText(LOOKUP_TEXT[i]);
	// Set listeners
	ImageButton continueBtn=(ImageButton) this.findViewById(R.id.mainButtonContinue);
	continueBtn.setOnClickListener(new OnClickListener() {
	    @Override
	    public void onClick(View v) {
		finish();
	    }
	});
	ImageButton buyBtn=(ImageButton) this.findViewById(R.id.mainButtonPremium);
	buyBtn.setOnClickListener(new OnClickListener() {
	    @Override
	    public void onClick(View v) {
		Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse(ADFREE_URL));
		startActivity(i);
		finish();
	    }
	});
	ImageButton voteBtn=(ImageButton) this.findViewById(R.id.mainButtonVote);
	voteBtn.setOnClickListener(new OnClickListener() {
	    @Override
	    public void onClick(View v) {
		Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse(VOTE_URL));
		startActivity(i);
		finish();
	    }
	});
    }

    private void goToAmazon(String appUrl) {
	PackageManager packageManager=getPackageManager();
	Intent intent=packageManager.getLaunchIntentForPackage(packageName);
	if (intent != null) {
	    // Amazon Appstore is installed: open it
	    startActivity(intent);
	}
	else {
	    // Open web page for dailyfree app
	    Intent ii=new Intent(Intent.ACTION_VIEW, Uri.parse(appUrl));
	    startActivity(ii);
	}
    }
    //
    // private void goToWebAmazon(String appName) {
    // Intent ii=new Intent(Intent.ACTION_VIEW, Uri.parse(AMAZON_URL + appName.replaceAll(" ",
    // "%20")));
    // startActivity(ii);
    // }
}